package by.grsu.shedule.fragment;

import java.util.Date;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import by.grsu.shedule.MyScheduleActivity;
import by.grsu.shedule.R;
import by.grsu.shedule.common.utils.GetSystemService;
import by.grsu.shedule.constants.Week;
import by.grsu.shedule.database.SheduleManager;
import by.grsu.shedule.util.CalendarHelper;

public class DateFragment extends Fragment {

	private static final String TAG = DateFragment.class.getSimpleName();

	private static final String ARG_QUERY = "query";

	private String[] weekItems;

	private TextView dateTextiView;

	private ImageButton selectWeekButton;

	private ImageButton refreshButton;

	private static Date currentWeek;

	public static DateFragment newInstance(String url, int dayNumber) {
		Bundle bundle = new Bundle();
		DateFragment fragment = new DateFragment();
		bundle.putString(ARG_QUERY, url);
		fragment.setArguments(bundle);
		return fragment;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.date, null);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		dateTextiView = (TextView) getView().findViewById(R.id.currentDate);
		dateTextiView.setText(CalendarHelper.convertDateForDateFragment(
				CalendarHelper.getThisStartWeek(),
				CalendarHelper.getThisFinishWeek()));
		currentWeek = CalendarHelper.getThisStartWeek();
		selectWeekButton = (ImageButton) getView().findViewById(
				R.id.selectWeekButton);
		selectWeekButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				weekItems = new String[2];
				weekItems[0] = CalendarHelper.convertDateForDateFragment(
						CalendarHelper.getThisStartWeek(),
						CalendarHelper.getThisFinishWeek());
				weekItems[1] = CalendarHelper.convertDateForDateFragment(
						CalendarHelper.getNextStartWeek(),
						CalendarHelper.getNextFinishWeek());
				DialogFragment dialogFragment = new SelectDateDialogFragment();
				dialogFragment.show(getFragmentManager(), TAG);
			}
		});

		refreshButton = (ImageButton) getView()
				.findViewById(R.id.refreshButton);
		refreshButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				MyScheduleActivity.getOnUpdateButtonClickListener().onRefreshButtonClickListener();
				
			}
		});
	}

	private class SelectDateDialogFragment extends DialogFragment {

		@Override
		public Dialog onCreateDialog(Bundle savedInstanceState) {
			return new AlertDialog.Builder(getActivity())
					.setTitle(R.string.selectWeek)
					.setItems(weekItems, new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							dateTextiView.setText(weekItems[which]);
							if (which == 0) {
								currentWeek = CalendarHelper.getThisStartWeek();
								MyScheduleActivity.getOnDataChangeListener()
										.onDateChangeListener();
							} else {
								currentWeek = CalendarHelper.getNextStartWeek();
								MyScheduleActivity.getOnDataChangeListener()
										.onDateChangeListener();
							}

						}
					}).create();
		}
	}

	public static Date getCurrentWeek() {
		return currentWeek;
	}
}
